<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background: url("../images/money.jpg") no-repeat;
            background-size: cover;
        }
        #box h1 {
            color: rgb(224, 20, 20);
        }
        #box {
            width: 500px;
            height: auto;
            margin: 0 auto;
            margin-top: 300px;
            border: 1px solid #999;
            text-align: center;
            background-color: #fff;
            border-radius: 20px;
        }
        #box p {
            font-size: 16px;
            font-weight: bold;
            color: rgb(31, 55, 187);
        }
        #box input {
            margin-left: 20px;
            width: 50%;
            border-radius: 15px;
            outline-style: none;
            padding-left: 10px;
            font-size: 20px;
        }
        #box a {
            display: inline-block;
            width: 100px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            background-color: rgb(235, 12, 4);
            text-decoration: none;
            color: #fff;
            border-radius: 15px;
        }
        #box h3 {
            color: rgb(33, 134, 202);
        }
    </style>
</head>

<body>
    <div id="box">
        <H1>密码修改</H1>
        <h3>账号：<span id="uname">
                <%= user.aname %>
            </span></h3>
        <p>
            输入新密码<input type="text" id="upaswd1">
        </p>
        <p>
            重新新密码<input type="text" id="upaswd2">
        </p>
        <p>
            <a id="sure" data-aid="<%= user.aid %>">确认修改</a>
            <a href="/admin/showadmin">取消返回</a>
        </p>
    </div>

    <script>
        document.getElementById('sure').onclick = function () {
            let upsValue1 = document.getElementById('upaswd1').value;

            let upsValue2 = document.getElementById('upaswd1').value;
            if (upsValue1 != upsValue2) {
                alert('俩次输入密码不一样')
                document.getElementById('upaswd1').value = '';
                document.getElementById('upaswd2').value = ''
                return

            }

            let aid = this.getAttribute('data-aid')
            let xhr = new XMLHttpRequest();
            xhr.open('POST', '/admin/asort', true)
            xhr.responseType = 'json'
            xhr.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded')
            xhr.send(`aid=${aid}&newpassword=${upsValue1}`)

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {

                    if (xhr.response.code == 200) {
                        alert("密码修改成功")

                        location.href = '/admin/showadmin';
                    } else {
                        alert("密码修改失败")
                        document.getElementById('upaswd1').value = '';
                        document.getElementById('upaswd2').value = ''
                    }
                }
            }
        }
    </script>
</body>

</html>